<?
class fileImageStorage extends bjImageStorage{
	
	const STATUS_FILE_ERROR = -1;

	const PLUGIN_INIT_SUCCESS 		= 100;
	const PLUGIN_ENABLE_SUCCESS		= 101;
	const PLUGIN_DISABLE_SUCCESS	= 102;
	const PLUGIN_DESTROY_SUCCESS	= 103;
	const PLUGIN_UPGRADE_SUCCESS	= 104;

	const PLUGIN_INIT_FAIL 			= -100;
	const PLUGIN_ENABLE_FAIL		= -101;
	const PLUGIN_DISABLE_FAIL		= -102;
	const PLUGIN_DESTROY_FAIL		= -103;
	const PLUGIN_UPGRADE_FAIL		= -104;
	const PLUGIN_UPGRADE_NONE		= -105;

	public function getPluginDescriptors(){
		return array(
			"title" => "Image File Storage",
			"guid" => "fileImageStorage",
			"type" => "imageStorage",
			"version" => "1.0",
			"authors" => array("Theo Chakkapark"),
			"website" => "http://suteki.nu",
			"description" => "Default storage mode for images for BlueJot. Stores images (and related data) to the filesystem."	
		);			
	}
		
	public function pluginInit(){
		return array("errorCode" => self::PLUGIN_INIT_SUCCESS, 'msg' => 'Init successfully');
	}
		
	public function pluginEnable($params){
		return array("errorCode" => self::PLUGIN_ENABLE_SUCCESS, 'msg' => 'Plugin enabled.');			
	}
	
	public function pluginDisable($params){
		return array("errorCode" => self::PLUGIN_DISABLE_FAIL, 'msg' => 'You cannot disable this plugin.');	
	}	
	
	public function pluginDestroy($params){
		return array("errorCode" => self::PLUGIN_DESTROY_FAIL, 'msg' => 'You cannot remove this plugin.');	
	}

	public function pluginUpgrade(){
		return array("errorCode" => self::PLUGIN_UPGRADE_NONE, 'msg' => 'Nothing to upgrade.');			
	}
	
	/**
	 * Saves image data to the datastore.
     * @todo Implement URL to file on return.
	 * @param array $params
	 * @subparam String $params:data[i]->extension Image type (ie PNG, JPG)
	 * @subparam Mixed  $params:data[i]->data Raw image data reflecting the type
	 * @subparam Integer $params:collaborator->collborator_id collaborator_id
	 * @subparam String [$params:data[i]->version] 
	 * @subparam String [$params:data[i]->filename]
	 * @subparam String [$params:->preCallback[i]->className] Class (with method name) to call before save
	 * @subparam String [$params:->preCallback[i]->methodName] 
	 * @subparam String [$params:->postCallback[i]->className] Class (with method name) to call post save.
	 * @subparam String [$params:->postCallback[i]->methodName] 
	 * @return array $return
	 * @subreturn String $return:status[i]->errorCode
	 * @subreturn String $return:status[i]->msg
	 * @subreturn String $return:files[i]->filename Name of the file, including extension
	 * @subreturn String $return:files[i]->filePath Path the file is located in
	 * @subreturn String $return:files[i]->extension Extension of the file
	 * @subreturn String $return:files[i]->basename The filename without the extension
	 * @subreturn String $return:files[i]->url The full URL to the file, including filename	 
	 */
	public function imageStorageSave($params){
		//Call precallbacks; examples where this might work might be to apply filters to an image before it goes to save
		$params["storageGuid"] = "fileImageStorage"; //set the plugin Guid
		 		
		/* ----- Save to filesystem ----- 
		   - extension would be the filetype that might identify the type of data it is
		   - data would be the raw data to save to a file
		*/
		
		//by default, data would be stored in /web/data/images/collborator_id
		$path = sfConfig::get('sf_web_dir').DIRECTORY_SEPARATOR.'data'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.$params["collaborator"]["collborator_id"].DIRECTORY_SEPARATOR;
		$timestamp = time(); //Use as a label in the event no filename is specified
		
		$data["files"] = array(); //array of files
		
		foreach($params["data"] as $d){
			//Assemble the file name
			$filename = bjUtil::nvl($d["version"], '').bjUtil::nvl($d["filename"], $timestamp).'.'.$d["extension"];
			//Write the file
			$fileinfo = array(
				"filename" => $filename,
				"path" => $path,
				"extension" => $d["extension"],
				"basename" => basename($filename, $d["extension"])
			);

			try{
				$fh = fopen($path.$filename, 'w');		
				fwrite($fh, $d["data"]);
				fclose($fh);
				//Store the file information
				array_push($data["files"], $fileinfo);
			}catch(Exception $e){
				$ex["errorCode"] = self::STATUS_FILE_ERROR;
				$ex["msg"] = $e->getMessage();
				array_push($data["status"], $ex);
			}
		}
				
		return $data; 
	}
	
	/**
	 * Returns back raw data for use with an applet
	 * @param array $params
	 * @subreturn String $params:files[i]->filename Name of the file, including extension
	 * @subreturn String $params:files[i]->filePath Path the file is located in
	 * @subreturn String $params:files[i]->extension Extension of the file
	 * @subreturn String $params:files[i]->basename The filename without the extension
	 * @return array $return
	 * @subreturn String $return:status[i]->errorCode
	 * @subreturn String $return:status[i]->msg
	 * @subreturn String $return:files[i]->filePath Path the file is located in
	 * @subreturn String $return:files[i]->extension Extension of the file
	 * @subreturn String $return:files[i]->basename The filename without the extension
	 * @subreturn String $params:files[i]->filename Name of the file, including extension
	 * @subreturn String $params:files[i]->data Raw data associated with the file	 	 
	 */
	public function imageStorageLoad($params){
			
	}	
}

?>